#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/**
 * 标准字符串函数
 */
int main() {

	int pos, len, ia, ib;
	char hello[50] = "Good", token_separator[] = "!,\n \t...";
	char *pa, *pb, *pc;
	double da;
	long la;
	unsigned long ula;
	FILE *input_stream;

	printf("/********** A - function atoi ********************/\n");
	// 将字符串转为 int
	ia = atoi("-123.45xyz");
	printf("A--- atoi() converts -123.45xyz to ia = %d\n\n", ia);

	printf("/*********** B - function atof ****************/\n");
	// 将字符串转为 double
	da = atof("-987.65E+01pqr");
	printf("B--- atof() converts -987.65E+01pqr to da = %lf\n\n", da);

	printf("/******** C - function atol *****************/\n");
	// 将字符串转为 long
	la = atol("-456.89abc");
	printf("C--- atol() converts -456.89abc to la = %ld\n\n", la);

	printf("/******** D function strcat ****************/\n");
	// 将第二个字符串的拷贝追加到第一个字符串后面，返回地址
	pa = strcat(hello, " morning!");
	printf("D--- hello = %s $$$ String at pa = %s\n\n", hello, pa);

	printf("/******** E - function strchr ****************/\n");
	// 查找指定第一个的字符
	pb = strchr(hello, 'm');
	// 计算出现的位置
	pos = pb - hello + 1;
	printf("E--- Character 'm' is the %dth character of string %s, string at"
		   "pb = %s\n\n", pos, hello, pb);

	printf("/******* F - function strcmp ******************/\n");
	// 按照字典模式比较，返回一个int值
	ia = strcmp(hello, "Good xyz");
	// ia < 0，第一个字符串小于第二个字符串
	if (ia < 0) {
		printf("F--- ia = %d, %s is less than Good xyz!\n\n", ia, hello);
	}
	// ia == 0， 第一个字符串等于第二个字符串
	if (ia == 0) {
		printf("F--- ia = %d, %s is identical to Good xyz!\n\n", ia, hello);
	}
	// ia > 0，第一个字符串大于第二个字符串
	if (ia > 0) {
		printf("F--- ia = %d, %s is greater than Good xyz!\n\n", ia, hello);
	}

	printf("/******* H - function strcspn ********************/\n");
	// 找出第一次出现指定字符串的位置
	pos = strcspn(hello, "dog");
	printf("H--- The first occurrence of any character in substring, dog "
		   "is string %s is the %dnd character\n\n", hello, pos + 1);

	printf("/********* I - function strerror *******************/\n");
	// 非0时表示存在异常
	errno = 0;
	input_stream = fopen("abcdefgh.ijk", "r");
	if (errno) {
		pa = strerror(errno);
		printf("I--- Does file abcdefgh.ijk exist? strerror() says %s\n\n", pa);
	}

	printf("/********* J - function strlen ************************/\n");
	// 返回字符数，不包含字符串的截止符
	len = strlen(hello);
	printf("J--- Not including the null character, %s has %d characters\n\n",
		   hello, len);

	printf("/********** K - function strncat ************************/\n");
	// 截取第二个字符串指定字符追加到第一个字符
	pb = strncat(hello, " John. How are you!", 5);
	printf("K--- hello = %s $$$ String at pb = %s\n\n", hello, pb);

	printf("/********** L - function strncmp *********************/\n");
	// 将第一个字符串前15个字符和第二个字符串按照字典比较
	ib = strncmp(hello, "Good cards", 15);
	if (ib > 0) {
		printf("L--- ib = %d, %s is greater than Good car\n\n",
			   ib, hello);
	}

	printf("/********** M - function strncpy ********************/\n");
	// 将第二个字符串前6个字符拷贝，表达式 hello + 14 利用指针
	pa = strncpy(hello + 14, "Linda. How are you!", 6);
	printf("M--- hello = %s $$$ String at pa = %s\n\n", hello, pa);

	printf("/********** N - function strpbrk ********************/\n");
	// 扫描第一个字符串，是否包含第二个字符串的任何字符
	pb = strpbrk(hello, "dear");
	printf("N--- hello = %s $$$ String at pa = %s\n\n", hello, pb);

	printf("/********** O - function strrchr ********************/\n");
	// 扫描第一个字符串，确定字符最后一次出现的位置
	pa = strrchr(hello, 'm');
	printf("O--- hello = %s $$$ String at pa = %s\n\n", hello, pa);

	printf("/********** P - function strspn *********************/\n");
	ia = strspn("Good year", hello);
	printf("P--- The %dth character 'y' is the first character in Good year\n"
		   "that is no present in %s\n\n", ia + 1, hello);

	printf("/********* Q - function strstr *********************/\n");
	// 在第一个字符串中找出第二个字符串所在的位置
	pa = strstr(hello, "Linda");
	ia = pa - hello + 1;
	printf("Q--- Linda was found at position %d of %s @@@ String at pa = %s\n\n",
		   ia, hello, pa);

	printf("/********* R - function strtod ******************/\n");
	// 将字符串转为double，并返回非法字符
	da = strtod("123.45abc", &pb);
	printf("R--- Find double number %lf in 123.45abc $$$ String at pb = %s\n\n",
		   da, pb);

	printf("/********** S - function strtol *****************/\n");
	// 将字符串转为long，返回非法字符，提供一个基数（进制）
	la = strtol("98765xyz", &pa, 10);
	printf("S--- Find long number %ld in 98765xyz $$$ String pa = %s\n\n",
		   la, pa);

	printf("/********* T - function strtoul *******************/\n");
	ula = strtoul("45678pqr", &pc, 10);
	printf("T--- Find unsigned long %ld in 45678pqr $$$ String at pc = %s\n\n",
		   ula, pc);

	printf("/********** U - function strtok *******************/\n");
	printf("hello = %s, token_separator = %s\n", hello, token_separator);
	// 查找分隔符
	pa = strtok(hello, token_separator);
	while (pa != NULL) {
		printf("U--- String at pa = %s pa = %p\n", pa, pa);
		pa = strtok(NULL, token_separator);
	}


}